package gdconf

import (
	"server_cluster/common/logger"
)

// 家园建筑等级组配置表

// 建筑类型
const (
	BuildingTypeFactory = 2 // 工厂
	BuildingTypeFarm    = 3 // 农田
)

type HomeBuildingLevelGroupConfig struct {
	BuildingLevelGroup int32    `csv:"Type" gorm:"primary_key;autoIncrement:false"`            // 建筑等级组
	Level              int32    `csv:"Level,omitempty" gorm:"primary_key;autoIncrement:false"` // 等级
	CostItemList       ItemList `csv:"Cost,omitempty"`                                         // 所需材料
	AddHomeExp         int32    `csv:"ProvideProsperity,omitempty"`                            // 增加家园经验
	BuildingType       int32    `csv:"Detail,omitempty"`                                       // 建筑类型
	Param1             int32    `csv:"param1,omitempty"`                                       // 参数1
	Param2             int32    `csv:"param2,omitempty"`                                       // 参数2
	Param3             int32    `csv:"param3,omitempty"`                                       // 参数3
}

func (h *HomeBuildingLevelGroupConfig) TableName() string {
	return "config_home_building_level_group"
}

func (g *GameDataConfig) saveHomeBuildingLevelGroupConfig() {
	saveTableToDb[HomeBuildingLevelGroupConfig](g.Db, readTable[HomeBuildingLevelGroupConfig](g.CsvPathPrefix+"HomeBuildGroup.csv"))
}

func (g *GameDataConfig) loadHomeBuildingLevelGroupConfig() {
	g.GameDataMaps.HomeBuildingLevelGroupConfigMap = make(map[int32]map[int32]*HomeBuildingLevelGroupConfig)
	for _, homeBuildingLevelGroupConfig := range loadTableFromDb[HomeBuildingLevelGroupConfig](g.Db) {
		_, exist := g.GameDataMaps.HomeBuildingLevelGroupConfigMap[homeBuildingLevelGroupConfig.BuildingLevelGroup]
		if !exist {
			g.GameDataMaps.HomeBuildingLevelGroupConfigMap[homeBuildingLevelGroupConfig.BuildingLevelGroup] = make(map[int32]*HomeBuildingLevelGroupConfig)
		}
		g.GameDataMaps.HomeBuildingLevelGroupConfigMap[homeBuildingLevelGroupConfig.BuildingLevelGroup][homeBuildingLevelGroupConfig.Level] = homeBuildingLevelGroupConfig
	}
	logger.Info("HomeBuildingLevelGroupConfig Count: %v", len(g.GameDataMaps.HomeBuildingLevelGroupConfigMap))
}

func GetHomeBuildingLevelGroupConfigByGroupAndLevel(group int32, level int32) *HomeBuildingLevelGroupConfig {
	value1, exist := CONF.GameDataMaps.HomeBuildingLevelGroupConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] HomeBuildingLevelGroupConfig, group: %v, level: %v", group, level)
		return nil
	}
	value2, exist := value1[level]
	if !exist {
		logger.Error("[ConfigNotFound] HomeBuildingLevelGroupConfig, group: %v, level: %v", group, level)
		return nil
	}
	return value2
}

func GetHomeBuildingLevelGroupConfigMap() map[int32]map[int32]*HomeBuildingLevelGroupConfig {
	return CONF.GameDataMaps.HomeBuildingLevelGroupConfigMap
}
